package com.pushtorefresh.storio2.sample.db.entities;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.pushtorefresh.storio2.contentresolver.annotations.StorIOContentResolverColumn;
import com.pushtorefresh.storio2.contentresolver.annotations.StorIOContentResolverType;
import com.pushtorefresh.storio2.sample.db.tables.TweetsTable;
import com.pushtorefresh.storio2.sample.provider.meta.TweetMeta;
import com.pushtorefresh.storio2.sqlite.annotations.StorIOSQLiteColumn;
import com.pushtorefresh.storio2.sqlite.annotations.StorIOSQLiteType;


// Just a class for demonstration, real Tweet structure is more complex.

// This annotation will trigger annotation processor
// Which will generate type mapping code in compile time,
// You just need to link it in your code.
@StorIOSQLiteType(table = TweetsTable.TABLE, generateTableClass = false)
@StorIOContentResolverType(uri = TweetMeta.URI_STRING)
public class Tweet {

    /**
     * If object was not inserted into db, id will be null
     */
    @Nullable
    @StorIOSQLiteColumn(name = TweetsTable.COLUMN_ID, key = true)
    @StorIOContentResolverColumn(name = TweetsTable.COLUMN_ID, key = true)
    Long id;

    @NonNull
    @StorIOSQLiteColumn(name = TweetsTable.COLUMN_AUTHOR)
    @StorIOContentResolverColumn(name = TweetsTable.COLUMN_AUTHOR)
    String author;

    @NonNull
    @StorIOSQLiteColumn(name = TweetsTable.COLUMN_CONTENT)
    @StorIOContentResolverColumn(name = TweetsTable.COLUMN_CONTENT)
    String content;

    // leave default constructor for AutoGenerated code!
    Tweet() {
    }

    private Tweet(@Nullable Long id, @NonNull String author, @NonNull String content) {
        this.id = id;
        this.author = author;
        this.content = content;
    }

    @NonNull
    public static Tweet newTweet(@Nullable Long id, @NonNull String author, @NonNull String content) {
        return new Tweet(id, author, content);
    }

    @NonNull
    public static Tweet newTweet(@NonNull String author, @NonNull String content) {
        return new Tweet(null, author, content);
    }

    @Nullable
    public Long id() {
        return id;
    }

    @NonNull
    public String author() {
        return author;
    }

    @NonNull
    public String content() {
        return content;
    }

    // BTW, you can use AutoValue/AutoParcel to get immutability and code generation for free
    // Check our tests, we have examples!
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Tweet tweet = (Tweet) o;

        if (id != null ? !id.equals(tweet.id) : tweet.id != null) return false;
        if (!author.equals(tweet.author)) return false;
        return content.equals(tweet.content);
    }

    @Override
    public int hashCode() {
        int result = id != null ? id.hashCode() : 0;
        result = 31 * result + author.hashCode();
        result = 31 * result + content.hashCode();
        return result;
    }

    @Override
    public String toString() {
        return "Tweet{" +
                "id=" + id +
                ", author='" + author + '\'' +
                ", content='" + content + '\'' +
                '}';
    }
}
